<?php
	function getDateTimeNow($fotmat){ // fix start with year yyyy define only after yyyy such as getDateTtimeNow("-m-d")
	  	$tempY=date("Y");
		if($tempY<"2550"){
			$tempY=(int)$tempY+543;
		}
		$datetime_now= $tempY.date($fotmat);
		
		return $datetime_now;
	}
	
	function getDateTimeYMD($d){
		return substr($d,0,4)."-".substr($d,4,2)."-".substr($d,6,2);
	}
	
	function getDateTimeDMY($d){
		return substr($d,6,2)."-".substr($d,4,2)."-".substr($d,0,4);
	}
	
	function getMenu($tab,$user_type){//return menu itemp
	
		if($user_type=="") $user_type="1";// ถ้ายังไม่ login
		
		$arrUserPermit = array (
  "1"  => array ("0" => 1, "gis" => 1, "event" => 1, "victim" => 1, "relief" => 0, "esearch" => 0, "vsearch" => 0,  "apply" => 1,"chngpwd" => 0,"report" => 0,"mtrgetdata" => 0,"manageuser" => 0),
  "2"  => array ("0" => 1, "gis" => 1, "event" => 1, "victim" => 1, "relief" => 0, "esearch" => 1, "vsearch" => 1,  "apply" => 0,"chngpwd" => 1,"report" => 0,"mtrgetdata" => 0,"manageuser" => 0),
  "3"  => array ("0" => 1, "gis" => 1, "event" => 1, "victim" => 1, "relief" => 0, "esearch" => 1, "vsearch" => 1,  "apply" => 0,"chngpwd" => 1,"report" => 0,"mtrgetdata" => 0,"manageuser" => 0),
  "4"  => array ("0" => 1, "gis" => 1, "event" => 1, "victim" => 1, "relief" => 0, "esearch" => 1, "vsearch" => 1,  "apply" => 0,"chngpwd" => 1,"report" => 1,"mtrgetdata" => 0,"manageuser" => 0),
  "5"  => array ("0" => 1, "gis" => 1, "event" => 1, "victim" => 1, "relief" => 0, "esearch" => 1, "vsearch" => 1,  "apply" => 0,"chngpwd" => 1,"report" => 1,"mtrgetdata" => 0,"manageuser" => 1)
);
			
		$arrtab = array("0"=>"","mtrgetdata"=>"","logout"=>"","dldataset"=>"","event"=>"","esearch"=>"","vsearch"=>"","manageuser"=>"","victim"=>"","relief"=>"","chngpwd"=>"","apply"=>"","about"=>"","report"=>"");
		switch ($tab) { 
    		case "gis":    
				$arrtab["gis"]=" class=\"current_page_item\"";
    			break; 
    		case "event":    
				$arrtab["event"]=" class=\"current_page_item\"";
    			break; 
			case "victim":    
				$arrtab["victim"]=" class=\"current_page_item\"";
    			break;
			case "relief":    
				$arrtab["relief"]=" class=\"current_page_item\"";
    			break; 
			case "esearch":    
				$arrtab["esearch"]=" class=\"current_page_item\"";
    			break;
			case "vsearch":    
				$arrtab["vsearch"]=" class=\"current_page_item\"";
    			break;
    		case "mtrgetdata":  
				$arrtab["mtrgetdata"]=" class=\"current_page_item\"";
    			break; 
    		case "manageuser":  
				$arrtab["manageuser"]=" class=\"current_page_item\"";
    			break; 
    		case "chngpwd":    
				$arrtab["chngpwd"]=" class=\"current_page_item\"";
    			break; 
			//case "logout":
			//	$arrtab["logout"]=" class=\"current_page_item\"";
    		//	break;
			case "apply":
				$arrtab["apply"]=" class=\"current_page_item\"";
    			break;
			case "report":
				$arrtab["report"]=" class=\"current_page_item\"";
    			break; 
    		default:    
				$arrtab["0"]=" class=\"current_page_item\"";
				break; 
		} 
		$menu = "";
		if($arrUserPermit[$user_type]["0"]){
			$menu = $menu."\n<li".$arrtab["0"]."><a href=\"index.php?tab=0\">หน้าแรก</a></li>";
		}
		if($arrUserPermit[$user_type]["gis"]){
			$menu = $menu."\n<li".$arrtab["gis"]."><a href=\"index.php?tab=gis\">สอบถามเชิงพื้นที่</a></li>";
		}
		if($arrUserPermit[$user_type]["event"]){
			$menu = $menu."\n<li".$arrtab["event"]."><a href=\"index.php?tab=event\">สอบถามเหตุการณ์</a></li>";
		}
		if($arrUserPermit[$user_type]["victim"]){
			$menu = $menu."\n<li".$arrtab["victim"]."><a href=\"index.php?tab=victim\">สอบถามผู้ได้รับผลกระทบ</a></li>";
		}
		if($arrUserPermit[$user_type]["relief"]){
			$menu = $menu."\n<li".$arrtab["relief"]."><a href=\"index.php?tab=relief\">สอบถามการเยียวยา</a></li>";
		}
		/*if($arrUserPermit[$user_type]["esearch"]){
			$menu = $menu."\n<li".$arrtab["esearch"]."><a href=\"index.php?tab=esearch\">สืบค้นเหตุการณ์</a></li>";
		}
		if($arrUserPermit[$user_type]["vsearch"]){
			$menu = $menu."\n<li".$arrtab["vsearch"]."><a href=\"index.php?tab=vsearch\">สืบค้นผู้ได้รับผลกระทบ</a></li>";
		}*/
		if($arrUserPermit[$user_type]["report"]){
			$menu = $menu."\n<li".$arrtab["report"]."><a href=\"index.php?tab=report\">รายงานเพิ่มเติม</a></li>";
		}
		if($arrUserPermit[$user_type]["manageuser"]){
			$menu = $menu."\n<li".$arrtab["manageuser"]."><a href=\"index.php?tab=manageuser\">ข้อมูลผู้ใช้</a></li>";
		}
		if($arrUserPermit[$user_type]["mtrgetdata"]){
			$menu = $menu."\n<li".$arrtab["mtrgetdata"]."><a href=\"index.php?tab=mtrgetdata\">ผลการดึงข้อมูล</a></li>";
		}
		if($arrUserPermit[$user_type]["chngpwd"]){
			$menu = $menu."\n<li".$arrtab["chngpwd"]."><a href=\"index.php?tab=chngpwd\">เปลี่ยนรหัสผ่าน</a></li>";
		}
		//if($arrUserPermit[$user_type]["logout"]){
			//$menu = $menu."\n<li".$arrtab["logout"]."><a href=\"index.php?tab=logout\">ออกจากระบบ</a></li>";
		//}
		
		if($arrUserPermit[$user_type]["apply"]){
			$menu = $menu."\n<li".$arrtab["apply"]."><a href=\"index.php?tab=apply\">การสมัครใช้งาน</a></li>";
		}
		return $menu;
	}
	
// convert UTM to lat lng
function LatLonPointUTMtoLL($f, $f1, $j=47) {
//$f = tary ใน UTM
//$f1 = tarx ใน UTM
// $f = Northing
// $f1 = Easting
// $j: UTM ZONE (Danmark = 32)

$d = 0.99960000000000004; // scale along long0
$d1 = 6378137; // Polar Radius
$d2 = 0.0066943799999999998;

$d4 = (1 - sqrt(1 - $d2)) / (1 + sqrt(1 - $d2));
$d15 = $f1 - 500000;
$d16 = $f;
$d11 = (($j - 1) * 6 - 180) + 3;
$d3 = $d2 / (1 - $d2);
$d10 = $d16 / $d;
$d12 = $d10 / ($d1 * (1 - $d2 / 4 - (3 * $d2 * $d2) / 64 - (5 * pow($d2,3) ) / 256));
$d14 = $d12 + ((3 * $d4) / 2 - (27 * pow($d4,3) ) / 32) * sin(2 * $d12) + ((21 * $d4 * $d4) / 16 - (55 * pow($d4,4) ) / 32) * sin(4 * $d12) + ((151 * pow($d4,3) ) / 96) * sin(6 * $d12);
$d13 = rad2deg($d14);
$d5 = $d1 / sqrt(1 - $d2 * sin($d14) * sin($d14));
$d6 = tan($d14) * tan($d14);
$d7 = $d3 * cos($d14) * cos($d14);
$d8 = ($d1 * (1 - $d2)) / pow(1 - $d2 * sin($d14) * sin($d14), 1.5);
$d9 = $d15 / ($d5 * $d);
$d17 = $d14 - (($d5 * tan($d14)) / $d8) * ((($d9 * $d9) / 2 - (((5 + 3 * $d6 + 10 * $d7) - 4 * $d7 * $d7 - 9 * $d3) * pow($d9,4) ) / 24) + (((61 + 90 * $d6 + 298 * $d7 + 45 * $d6 * $d6) - 252 * $d3 - 3 * $d7 * $d7) * pow($d9,6) ) / 720);
$d17 = rad2deg($d17); // Breddegrad (N)
$d18 = (($d9 - ((1 + 2 * $d6 + $d7) * pow($d9,3) ) / 6) + (((((5 - 2 * $d7) + 28 * $d6) - 3 * $d7 * $d7) + 8 * $d3 + 24 * $d6 * $d6) * pow($d9,5) ) / 120) / cos($d14);
$d18 = $d11 + rad2deg($d18); //
return array('lat'=>$d17,'lng'=>$d18);
}
?>